<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加商品</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- Tab栏 -->
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="100px"
        label-position="top"
      >
        <el-tabs
          
          :tab-position="'left'"
          :before-leave="beforeTabLeave"
          
        >
          <el-tab-pane label="基本信息" name="0">
            <el-form-item label="商品名称" prop="name">
              <el-input v-model="addForm.name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格" prop="price">
              <el-input v-model="addForm.price" type="number"></el-input>
            </el-form-item>
            <el-form-item label="发布者" prop="poster">
              <el-input v-model="addForm.poster"></el-input>
            </el-form-item>
            <el-form-item label="商品描述" prop="description">
              <el-input v-model="addForm.description"></el-input>
            </el-form-item>
            <el-form-item label="商品分类" prop="sort">
              <el-cascader
                v-model="addForm.sort"
                :options="cateList"
                :props="cascaderProps"
                @change="handleChange"
              ></el-cascader>
            </el-form-item>
          </el-tab-pane>
            
            <!-- 添加商品 -->
            <el-button type="primary" class="btnAdd" @click="addGoods">添加商品</el-button>
          </el-tab-pane>
        </el-tabs>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import _ from 'lodash'
export default {
  data () {
    return {
      // 添加商品的表单对象
      addForm: {
        name: '',
        price: 0,
        poster: '',
        description: '',
        sort: '',
      },
      addFormRules: {
        name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '请输入商品价格', trigger: 'blur' }
        ],
        poster: [
          { required: true, message: '请输入发布者', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入商品描述', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请选择商品分类', trigger: 'blur' }
        ]
      },
      // 商品列表
      cateList: [ 

            {

              cat_id: '数码电子',

              cat_name: '数码电子'

            }, {

              cat_id: '日用百货',

              cat_name: '日用百货'

            }, {

              cat_id: '化妆品',

              cat_name: '化妆品'

            }, {

              cat_id: '食品',

              cat_name: '食品'

            },{

              cat_id: '书籍',

              cat_name: '书籍'

            },{

              cat_id: '文具',

              cat_name: '文具'

            },{

              cat_id: '玩具',

              cat_name: '玩具'

            }],
      // 级联选择器配置
      cascaderProps: {
        expandTrigger: 'hover',
        label: 'cat_name',
        value: 'cat_id',
        children: 'children'
      },
    }
  },
  created () {
    
  },
  /*computed: {
    getCateId () {
      if (this.addForm.goods_cat.length === 3) {
        return this.addForm.goods_cat[2]
      }
      return null
    }
  },*/
  methods: {
    // 级联选择器选中项变化时出发
    handleChange () {
      if (this.addForm.sort.length !== 1) {
        this.addForm.sort = []
      }
    },
    beforeTabLeave (activeName, odlActiveName) {
      // 未选中商品分类阻止Tab标签跳转
      if (odlActiveName === '0' && this.addForm.sort.length !== 1) {
        this.$message.error('请先选择商品分类')
        return false
      }
    },
    // 添加商品
    addGoods () {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return this.$message.error('请填写必要的表单项！')
        // 发送请求前：需对提交的表单进行处理goods_cat attrs
        // this.addForm.goods_cat = this.addForm.goods_cat.join(',')
        // 以上写法不对：级联选择器绑定的对象goods_cat要求是数组对象
        // 解决办法: 包：lodash 方法（深拷贝）：cloneDeep(boj)

        // 将this.addForm进行深拷贝
        const form = _.cloneDeep(this.addForm)
        form.sort = form.sort.join(',')
        // 发起请求添加商品
        // 商品名称必须是唯一的
        const { data: res } = await this.$http.post('addproducts', form)
		console.log(form)
        this.$message.success('添加商品成功!')		
        this.$router.push('/market')
      })
    }
  }
}
</script>

<style lang='less' scoped>
.el-checkbox {
  margin: 0 8px 0 0 !important;
}
.previewImg{
  width: 100%;
}
.btnAdd{
  margin-top: 15px
}
</style>
